//
//  ZYIndexDeviceItemView.m
//  YiLin
//
//  Created by Zyark on 2018/6/8.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYIndexDeviceItemView.h"
#import "ZYDevice.h"

@interface ZYIndexDeviceItemView()
//@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIImageView *deviceTypeImageView;
@property(nonatomic,strong) UILabel *deviceTypeLabel;

/**
 设备名称
 */
@property(nonatomic,strong) UILabel *nameLabel;

/**
 状态指标
 */
@property(nonatomic,strong) UILabel *statusLabel;
@property(nonatomic,strong) ZYChangeLanguage *language;
@end

@implementation ZYIndexDeviceItemView

-(instancetype)initWithFrame:(CGRect)frame withDeviceType:(ZYDeviceType)type{
    self = [super initWithFrame:frame];
    if (self) {
        _language = [ZYChangeLanguage sharedZYChangeLanguage];
        _type = type;
        [self setup];
    }
    return self;
}

-(void)setup{
    NSString *imageName = @"";
    switch (_type) {
        case ZYDeviceTypeAirConditioning:
            imageName = @"ic_index_main_device_ac";
            break;
        case ZYDeviceTypeRadiantFloorHeating:
            imageName = @"ic_index_main_device_rhf";
            break;
        case ZYDeviceTypeVentilationSystem:
            imageName = @"ic_index_main_device_vs";
            break;
        default:
            break;
    }
    CGFloat width = [UIImage imageNamed:@"ic_index_main_device_ac"].size.width;
    CGFloat margin = 0.05 * self.width;
    _deviceTypeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [self addSubview:_deviceTypeImageView];
    _deviceTypeImageView.width = width;
    _deviceTypeImageView.centerY = self.centerY;
    _deviceTypeImageView.left = margin;
    
    _deviceTypeLabel = [self setupLabel];
    _deviceTypeLabel.width = self.width * 0.18;
    [self addSubview:_deviceTypeLabel];
    _deviceTypeLabel.centerY = self.centerY;
    _deviceTypeLabel.left = _deviceTypeImageView.right + margin;
    _deviceTypeLabel.textAlignment = NSTextAlignmentLeft;
    
    _statusLabel = [self setupLabel];
    [self addSubview:_statusLabel];
    _statusLabel.width = self.width * 0.18;
    _statusLabel.centerY = self.centerY;
    _statusLabel.left = _deviceTypeLabel.right + 5;
    _statusLabel.textAlignment = NSTextAlignmentLeft;
    
    _nameLabel = [self setupLabel];
    _nameLabel.width = self.width -_statusLabel.right - margin;
    [self addSubview:_nameLabel];
    _nameLabel.centerY = self.centerY;
    _nameLabel.right = self.width - 15;
    _nameLabel.textAlignment = NSTextAlignmentRight;
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doClick)];
    [self addGestureRecognizer:tap];
}

-(void)setupTitle{
    NSString *title = @"";
    switch (_type) {
        case ZYDeviceTypeAirConditioning:
            title = LC_DEVICE_TYPE_AIR_CONDITIONING;
            break;
        case ZYDeviceTypeRadiantFloorHeating:
            title = LC_DEVICE_TYPE_RADIANT_FLOOR_HEATING;
            break;
        case ZYDeviceTypeVentilationSystem:
            title = LC_DEVICE_TYPE_VENTILATION_SYSTEM;
            break;
        default:
            break;
    }
    _deviceTypeLabel.text = [_language getStringByKey:title];
}

-(void)setDevice:(ZYDevice *)device{
    _device = device;
    if(device.type != ZYDeviceTypeVentilationSystem){
        _statusLabel.text = [NSString stringWithFormat:@"%@℃",device.roomTemperature];
    }else{
        _statusLabel.text = [NSString stringWithFormat:@"PM2.5:%@",device.outPmExponent];
    }
    _nameLabel.text = device.name;
    [self setupTitle];
}

-(void)doClick{
    if([_delegate respondsToSelector:@selector(indexDeviceItemView:didSelectDevice:)]){
        [_delegate indexDeviceItemView:self didSelectDevice:_device];
    }
}

-(UILabel *)setupLabel{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = RGBA(51, 51, 51, 1);
    label.font = [UIFont systemFontOfSize:16];
    label.height = 15;
    return label;
}
@end
